package com.xuedian.xuanxuan.core.util;


import com.xuedian.xuanxuan.core.entity.JSONResult;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import java.util.Date;
import java.util.Map;

/**
 * Created by mmh on 2016/12/29.
 */
public class ResultUtils {

    public static String toJsonString(JSONResult jsonResult){
        String res = "";
        try {
            //日期格式转换
            JsonConfig config = new JsonConfig();
            config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));

            JSONObject json = JSONObject.fromObject(jsonResult,config);//将java对象转换为json对象
            res = json.toString();
            System.out.print("\nres:"+res);
        }catch (Exception e){
            e.printStackTrace();
        }
        return res;
    }

    public static Map toJsonMap(JSONResult jsonResult){
        Map<String,Object> res = null;
        try {
            //日期格式转换
            JsonConfig config = new JsonConfig();
            config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));

            JSONObject json = JSONObject.fromObject(jsonResult,config);//将java对象转换为json对象
            res = json;
            System.out.print("\nres:"+json.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return res;
    }
}
